#include "serialportset.h"
#include <QMessageBox>
#include <QtDebug>
#include <QMutex>
#include "databaseoperate.h"

serialPortSet *serialPortSet::self = 0;
serialPortSet *serialPortSet::Instance()
{
    if (!self) {
        QMutex mutex;
        QMutexLocker locker(&mutex);
        if (!self) {
            self = new serialPortSet;
        }
    }

    return self;
}

serialPortSet::serialPortSet()
{
    serial = new QSerialPort;
    bytes = new ByteSet;
    //unconfirm
    connect(this, SIGNAL(getList2Byte(QStringList)), bytes, SLOT(setSuctionStrList(QStringList)));
    openSerialPort();
}
//打开串口
bool serialPortSet::openSerialPort()
{
    QString str;
    databaseOperate::Instance()->queryPortIni(str, 1);
    serial->setPortName(str);
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    if(serial->isOpen())
    {
        return true;
    }
    if(serial->open(QIODevice::ReadWrite))
    {
      serial->setDataTerminalReady(true);
    }
    else
    {
        QMessageBox::warning(NULL, "warning", "吸阻仪 Serial Port open failed!", QMessageBox::Yes, QMessageBox::Yes);
        return false;
    }
    connect(serial, SIGNAL(readyRead()), this, SIGNAL(readyRead()));
//    connect(serial, SIGNAL(readyRead()), this, SIGNAL(caliRead()));
}
//初始化吸阻仪
void serialPortSet::sendInit2Com()
{
    if(serial->isOpen())
    {
        serial->write(bytes->initEngine2ComByte());
    }
    else
    {
        emit initCaliFailed();
        emit initFailed();
        QMessageBox::warning(NULL, "warning", "初始化失败，请检查通信并重启程序或联系管理员!", QMessageBox::Yes, QMessageBox::Yes);
        return;
    }
}

//下发样品参数
void serialPortSet::sendSampleInfo2Com()
{
    serial->write(bytes->sampleInfoSend2Com());
}
//开始测量
void serialPortSet::sendStart2Com()
{
    serial->write(bytes->startTest2ComByte());
}
//系统参数
void serialPortSet::sendSystemInfo2Com()
{
    serial->write(bytes->systemInfoSend2Com());
}
//补充样品指令
void serialPortSet::sendAppendInfo2Com()
{
    serial->write(bytes->appendSample2Com());
}
//已清理料斗指令
void serialPortSet::sendCleanInfo2Com()
{
    serial->write(bytes->cleanBox2Com());
}
//停止指令
void serialPortSet::sendStop2Com()
{
    serial->write(bytes->stopTest2ComByte());
}
//校准
void serialPortSet::sendCaliZero2Com()
{
    serial->write(bytes->caliZero2Com());
}
//校准标准值
void serialPortSet::sendStandardValue2Com()
{
    serial->write(bytes->caliStandardValue2Com());
}
//取消校准
void serialPortSet::sendCaliCancle2Com()
{
    serial->write(bytes->caliCancle2Com());
}
//校准完成
void serialPortSet::sendCaliSuccess2Com()
{
    serial->write(bytes->caliSuccess2Com());
}
//校准验证
void serialPortSet::sendCaliCheck2Com()
{
    serial->write(bytes->caliCheck2Com());
}
//单步调试
void serialPortSet::send01on()
{
    serial->write(bytes->upTap());
}
void serialPortSet::send01off()
{
    serial->write(bytes->upTap0());
}

void serialPortSet::send02on()
{
    serial->write(bytes->downTap());
}
void serialPortSet::send02off()
{
    serial->write(bytes->downTap0());
}

void serialPortSet::send03on()
{
    serial->write(bytes->yansi());
}
void serialPortSet::send03off()
{
    serial->write(bytes->yansi0());
}

void serialPortSet::send04on()
{
    serial->write(bytes->lvzui());
}
void serialPortSet::send04off()
{
    serial->write(bytes->lvzui0());
}

void serialPortSet::send05on()
{
    serial->write(bytes->dingzhen());
}
void serialPortSet::send05off()
{
    serial->write(bytes->dingzhen0());
}

void serialPortSet::send06on()
{
    serial->write(bytes->yansitongfeng());
}
void serialPortSet::send06off()
{
    serial->write(bytes->yansitongfeng0());
}

void serialPortSet::send07on()
{
    serial->write(bytes->lvzuitongfeng());
}
void serialPortSet::send07off()
{
    serial->write(bytes->lvzuitongfeng0());
}

void serialPortSet::send08on()
{
    serial->write(bytes->tongfengxuanze());
}
void serialPortSet::send08off()
{
    serial->write(bytes->tongfengxuanze0());
}

void serialPortSet::send09on()
{
    serial->write(bytes->chouxi());
}
void serialPortSet::send09off()
{
    serial->write(bytes->chouxi0());
}
